package com.jingan.nursinghome.regulatory.select.timu

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import cn.android.support.v7.lib.sin.crown.base.BaseActivity
import cn.android.support.v7.lib.sin.crown.kotlin.anko.Titlebar
import cn.android.support.v7.lib.sin.crown.kotlin.base.BaseDialog
import cn.android.support.v7.lib.sin.crown.kotlin.common.px
import com.jingan.nursinghome.regulatory.model.Topic
import org.jetbrains.anko.*
import org.jetbrains.anko.recyclerview.v7.recyclerView
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.w3c.dom.Text
import java.util.ArrayList

/**
 * 养老机构端题目。只显示整改项。没有评分结结果页。
 * Created by 彭治铭 on 2018/7/21.
 */
class MicrometerTableOSDialog22(var activity: BaseActivity, var topics: ArrayList<Topic>, var iid: String, isStatus: Boolean = true, isTransparent: Boolean = true) : BaseDialog(activity, isStatus = isStatus, isTransparent = isTransparent) {

    init {
        setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right)
    }

    var recyclerView: RecyclerView? = null
    var topBar: Titlebar.TopBar? = null

    override fun onCreateView(context: Context): View? {

        return context.UI {
            verticalLayout {
                backgroundColor = Color.parseColor("#EEEEEE")
                //第三个参数是返回键的颜色。此时是透明色。即返回键无效
                topBar = Titlebar.topBar(context as Activity, "养老机构端",Color.GRAY)
                topBar?.centerTextView?.apply {
                    textColor = Color.BLACK
                }
                topBar?.leftTextView?.onClick {
                    dismiss()
                }
                topBar?.view?.setBackgroundColor(Color.WHITE)
                addView(topBar?.view)
                topBar?.shadow = true
//                topBar?.rightTextView?.apply {
//                    text = "登出"
//                    textColor = Color.BLACK
//                    onClick {
//                    }
//                    //lparams只能在anko布局里面使用
//                    //实例对象不能使用lparams，所以只能使用layoutParams
//                    layoutParams.apply {
//                        width = wrapContent
//                        height = wrapContent
//                    }
//                }

                recyclerView = recyclerView {
                    var linearLayoutManager = LinearLayoutManager(activity)
                    linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
                    layoutManager = linearLayoutManager
                }
            }
        }.view
    }

    override fun listener() {
        recyclerView?.apply {
            adapter = MyAdapter(topics)
        }
    }

    override fun recycleView() {
    }

    class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var tm_fulei: TextView? = null
        var tm_zilei: TextView? = null
        var title: TextView? = null
        var postion:TextView?=null

        init {
            tm_fulei = itemView?.findViewById(px.id("tm_fulei"))
            tm_zilei = itemView?.findViewById(px.id("tm_zilei"))
            title = itemView?.findViewById<TextView>(px.id("title"))
            postion=itemView?.findViewById(px.id("postion"))
        }
    }

    class MyAdapter(var topics: ArrayList<Topic>) : RecyclerView.Adapter<MyViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            var view = parent.context.UI {
                verticalLayout {
                    var layoutParams = ViewGroup.LayoutParams(px.realWidth().toInt(), wrapContent)
                    setLayoutParams(layoutParams)
                    gravity = Gravity.CENTER
                    verticalLayout {
                        backgroundColor = Color.WHITE
                        gravity = Gravity.CENTER_HORIZONTAL
                        textView {
                            id = px.id("tm_fulei")
                            textSize = px.textSizeX(36)
                            textColor = Color.BLACK
                        }.lparams {
                            topMargin = px.x(30)
                        }
                        textView {
                            id = px.id("tm_zilei")
                            textSize = px.textSizeX(30)
                            textColor = Color.BLACK
                        }.lparams {
                            topMargin = px.x(30)
                        }
                        linearLayout {
                            textView {
                                id = px.id("postion")
                                textSize = px.textSizeX(30)
                                textColor = Color.BLACK
                            }.lparams {}
                            textView {
                                id = px.id("title")
                                textSize = px.textSizeX(30)
                                textColor = Color.BLACK
                            }.lparams {}
                        }.lparams {
                            topMargin = px.x(30)
                            leftMargin = px.x(16)
                            rightMargin = px.x(16)
                            bottomMargin = px.x(30)
                            width = matchParent
                        }

                    }.lparams {
                        width = px.x(720)
                        height = wrapContent
                        topMargin = px.x(30)
                        //bottomMargin = px.x(30)
                    }
                }

            }.view
            return MyViewHolder(view)
        }

        override fun getItemCount(): Int {
            return topics.size
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            holder.tm_fulei?.setText(topics.get(position).tm_fulei)
            holder.tm_zilei?.setText(topics.get(position).tm_zilei)
            holder.postion?.setText((position+1).toString()+"、")
            holder.title?.setText(topics.get(position).title)
        }
    }

}